﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Objects;
using WISD.CarFriend.Model;
using WISD.Common;

namespace WISD.CarFriend.BLL.Web
{
    public class FriendLinkManageModule
    {
        /// <summary>
        /// 获取所有友情链接
        /// state:1 未删除
        /// state:0 已删除
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllFriendLinks()
        {
            using (CarFriendDBContext edm = new CarFriendDBContext())
            {
                return EFHelper.ListToDataTable<T_CF_Web_FriendLink>(edm.T_CF_Web_FriendLink.Where(p => p.State == 1).ToList());
            }
        }


        /// <summary>
        /// 根据ID查找友情链接
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public T_CF_Web_FriendLink GetFriendLinkByID(int id)
        {
            using (CarFriendDBContext edm = new CarFriendDBContext())
            {
                return edm.T_CF_Web_FriendLink.FirstOrDefault(p => p.FriendLinkID == id);
            }
        }


        /// <summary>
        /// 添加友情链接
        /// </summary>
        /// <param name="title"></param>
        /// <param name="imagePath"></param>
        /// <param name="url"></param>
        /// <param name="creatorID"></param>
        public void AddFriendLink(string title, string imagePath, string url, int creatorID)
        {
            using (CarFriendDBContext edm = new CarFriendDBContext())
            {
                T_CF_Web_FriendLink enterprise = new T_CF_Web_FriendLink()
                {
                    Title = title,
                    ImagePath = imagePath,
                    URL = url,
                    State = 1,
                    CreatorID = creatorID,
                    CreateTime = DateTime.Now,
                    UpdaterID = creatorID,
                    UpdateTime = DateTime.Now,
                    Remarks = ""
                };
                edm.T_CF_Web_FriendLink.AddObject(enterprise);
                edm.SaveChanges();
            }
        }


        /// <summary>
        /// 修改友情链接
        /// </summary>
        /// <param name="id"></param>
        /// <param name="title"></param>
        /// <param name="imagePath"></param>
        /// <param name="url"></param>
        /// <param name="updaterID"></param>
        public void UpdateFriendLink(int id, string title, string imagePath, string url, int updaterID)
        {
            using (CarFriendDBContext edm = new CarFriendDBContext())
            {
                T_CF_Web_FriendLink link = edm.T_CF_Web_FriendLink.FirstOrDefault(p => p.FriendLinkID == id);
                link.Title = title;
                link.ImagePath = imagePath;
                link.URL = url;
                link.UpdaterID = updaterID;
                link.UpdateTime = DateTime.Now;
                edm.SaveChanges();
            }
        }


        /// <summary>
        /// 删除友情链接
        /// </summary>
        /// <param name="id"></param>
        /// <param name="updaterID"></param>
        public void DelFriendLink(int id, int updaterID)
        {
            using (CarFriendDBContext edm = new CarFriendDBContext())
            {
                T_CF_Web_FriendLink link = edm.T_CF_Web_FriendLink.FirstOrDefault(p => p.FriendLinkID == id);
                link.State = 0;
                link.UpdaterID = updaterID;
                link.UpdateTime = DateTime.Now;
                edm.SaveChanges();
            }
        }


        /// <summary>
        /// 获取友情链接LinkXML (Image)
        /// </summary>
        /// <param name="top">显示条数</param>
        /// <returns></returns>
        public string GetLinkXMLForImage(int top)
        {
            StringBuilder xml = new StringBuilder();

            DataTable FLTable = new DataTable();
            using (CarFriendDBContext edm = new CarFriendDBContext())
            {
                string esql = "select value f from T_CF_Web_FriendLink as f where f.state=1 order by f.FriendLinkID skip 0 limit 10";
                ObjectQuery<T_CF_Web_FriendLink> query = edm.CreateQuery<T_CF_Web_FriendLink>(esql);
                FLTable = EFHelper.ListToDataTable<T_CF_Web_FriendLink>(query.Execute(MergeOption.NoTracking).ToList<T_CF_Web_FriendLink>());
            }

            if (FLTable.Rows.Count > 0)
            {
                for (int i = 0; i < FLTable.Rows.Count; i++)
                {
                    DataRow dr = FLTable.Rows[i];
                    xml.Append("<li>");
                    xml.Append("<a target=\"_blank\" href=\"" + dr["URL"].ToString() + "\">");
                    xml.Append("<img src=\"/Tools/ImageLoad.ashx?w=88&h=31&gurl=" + dr["ImagePath"].ToString() + "\" alt=\"" + dr["Title"].ToString() + "\" />");
                    xml.Append("</a>");
                    xml.Append("</li>\n");
                }
            }
            return xml.ToString();
        }


        /// <summary>
        /// 获取友情链接LinkXML (Text)
        /// </summary>
        /// <param name="top">显示条数</param>
        /// <returns></returns>
        public string GetLinkXMLForText(int top)
        {
            StringBuilder xml = new StringBuilder();

            DataTable FLTable = new DataTable();
            using (CarFriendDBContext edm = new CarFriendDBContext())
            {
                string esql = "select value f from T_CF_Web_FriendLink as f where f.state=1 order by f.FriendLinkID skip 0 limit " + top.ToString();
                ObjectQuery<T_CF_Web_FriendLink> query = edm.CreateQuery<T_CF_Web_FriendLink>(esql);
                FLTable = EFHelper.ListToDataTable<T_CF_Web_FriendLink>(query.Execute(MergeOption.NoTracking).ToList<T_CF_Web_FriendLink>());
            }

            if (FLTable.Rows.Count > 0)
            {
                for (int i = 0; i < FLTable.Rows.Count; i++)
                {
                    DataRow dr = FLTable.Rows[i];
                    xml.Append("<li>");
                    xml.Append("<a target=\"_blank\" title=\"" + dr["Title"].ToString() + "\" href=\"" + dr["URL"].ToString() + "\">" + dr["Title"].ToString() + "</a>");
                    xml.Append("</li>\n");
                }
            }
            return xml.ToString();
        }

    }
}

